home *** CD-ROM | disk | FTP | other *** search
- Path: maine.maine.edu!rwl380b
- Organization: University of Maine System
- Date: Tue, 30 Jan 1996 10:59:40 EST
- From: <RWL380B@MAINE.MAINE.EDU>
- Message-ID: <96030.105940RWL380B@MAINE.MAINE.EDU>
- Newsgroups: comp.lang.c++
- Subject: Help with float/double data types
-
- C++ Experts,
-
- I have a "simple" question re: floating point data types in C/C++.
- I've traditionally programmed in Fortran but am trying to learn
- new tricks.... I ran into this problem while trying to write a
- filter that converts the output of one commercial program to the
- input format for Arc/Info. A simplified version (just I/O) is
- included to illustrate my problems. Variables of interest in
- this discussion are: X, Y, Z, Zstd. This version of the program
- has been compiled in Turbo C++ v3.0 for DOS (Borland) and I've
- tried in Turbo C++ v4.5 for Windows (as an EasyWin program). The
- file I am reading is an ASCII version of interpolated values of
- a spatial process occurring across the US. (The program code and
- first lines of input file or at bottom of message).
-
- If I declare X, Y, Z and Zstd as float:
- float X=0.0,Y=0.0,Z=0.0,Zstd=0.0;
- Then the output looks correct but the digits after the decimal points
- in X are incorrect (original first line X = -2082085.85000). The
- first value of Y is incorrect, but the next values are correct!
-
-
- X-Coordinate Y-Coordinate Z-Est Z StdDev n
- -2082085.87500 -2127847.00000 23.12121 1.77931 8
- -2082085.87500 -2047847.12500 23.13800 1.73910 8
- -2082085.87500 -1967847.12500 23.15039 1.69751 8
- -2082085.87500 -1887847.12500 23.15789 1.65532 8
- -2082085.87500 -1807847.12500 23.15990 1.61336 8
- -2082085.87500 -1727847.12500 32.19845 1.55000 8
- -2082085.87500 -1647847.12500 38.46202 1.49013 8
- -2082085.87500 -1567847.12500 32.17017 1.45502 8
- -2082085.87500 -1487847.12500 39.51803 1.40543 8
- -2082085.87500 -1407847.12500 47.27689 1.37395 8
-
-
- If I declare X,Y,Z and Zstd to be double then the output is
- correct.
-
- X-Coordinate Y-Coordinate Z-Est Z StdDev n
- -2082085.85000 -2127847.12500 23.12121 1.77931 8
- -2082085.85000 -2047847.12500 23.13800 1.73910 8
- -2082085.85000 -1967847.12500 23.15039 1.69751 8
- -2082085.85000 -1887847.12500 23.15789 1.65532 8
- -2082085.85000 -1807847.12500 23.15990 1.61336 8
- -2082085.85000 -1727847.12500 32.19845 1.55000 8
- -2082085.85000 -1647847.12500 38.46202 1.49013 8
- -2082085.85000 -1567847.12500 32.17017 1.45502 8
- -2082085.85000 -1487847.12500 39.51803 1.40543 8
- -2082085.85000 -1407847.12500 47.27689 1.37395 8
-
- Coming from Fortran, this should not be happening. Borland's
- documentation suggests that negative floats and doubles don't
- exist. However, their web page has a technical document
- describing how floating points are represented and indicate
- that negative floats and doubles are OK. I am puzzled
- by the behavior I describe above. And yes, I need access to
- the X, Y and Z values as numbers for certain statistics I
- need to include so I can't just read them as strings.
- Any help is greatly Appreciated!
-
-
- // Program Reformat.cpp
- #include <iostream.h>
- #include <fstream.h>
-
- int main()
- {
- float X=0.0, Y=0.0, Z=0.0, Zstd=0.0; // coordinates & estimates.
- int N=0;
- char linebuf[80];
- fstream filin;
-
- filin.open("519070.blk", ios::in);
- cout.precision(5);
- cout.setf(ios::fixed | ios::showpoint);
-
-
- for (i = 0; i < 10; i++ ) {
- filin >> X >> Y >> Z >> Zstd >> N;
- cout << X << '\t' << Y << '\t' << Z << '\t' << Zstd << '\t' << N << '\n';
- }
-
- filin.close();
- return 1;
- }
-
-
- INPUT FILE: 519070.blk
-
- -2082085.85000 -2127847.12500 23.12121 1.77931 8
- -2082085.85000 -2047847.12500 23.13800 1.73910 8
- -2082085.85000 -1967847.12500 23.15039 1.69751 8
- -2082085.85000 -1887847.12500 23.15789 1.65532 8
- -2082085.85000 -1807847.12500 23.15990 1.61336 8
- -2082085.85000 -1727847.12500 32.19845 1.55000 8
- -2082085.85000 -1647847.12500 38.46202 1.49013 8
- -2082085.85000 -1567847.12500 32.17017 1.45502 8
- -2082085.85000 -1487847.12500 39.51803 1.40543 8
- -2082085.85000 -1407847.12500 47.27689 1.37395 8
- <2173 linese deleted>
-
-
- Thanks,
- Tim Jones
- Research Associate
- Dept of Wildlife Ecology
- University of Maine
- Orono, ME 04418
- tim@wlm2.umenfa.maine.edu
-
- PS - sorry for the length of this message!
-